import fs from 'fs';
import { resolve } from 'path';

function deleteBeforeBuildPlugin(options = {}) {
  const { targets = [] } = options;

  return {
    name: 'delete-before-build',

    buildStart() {
      targets.forEach((target) => {
        const resolvedPath = resolve(target);

        if (fs.existsSync(resolvedPath)) {
          fs.rmSync(resolvedPath, { recursive: true, force: true });
          console.log(`Deleted: ${resolvedPath}`);
        } else {
          console.log(`Path does not exist: ${resolvedPath}`);
        }
      });
    }
  };
}

export default deleteBeforeBuildPlugin;
